<template>
  <el-dialog title="角色分配" :visible="showAssignDialog" @close="close">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{ item.title }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain size="small" @click="submit">确定</el-button>
      <el-button plain size="small" @click="close">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/roles'
import { employeeDetails, updateEmployee } from '@/api/employee'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      rolesList: [], // 角色列表
      checkList: [] // 选中的角色
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    // 获取角色列表
    async getRole() {
      const res = await getRoles()
      this.rolesList = res.data
    },
    // 获取详情 -- 角色数据回显
    employeeDetails() {
      if (this.id) {
        this.$nextTick(async() => {
          const res = await employeeDetails(this.id)
          if (res.data.roles.length) {
            this.checkList = res.data.roles.map(item => item.id)
          }
        })
      }
    },
    // 确认分配角色按钮
    async submit() {
      await updateEmployee({
        id: this.id,
        roles: this.checkList
      })
      //   提示
      this.$message.success('角色分配成功')
      // 关闭弹窗
      this.close()
    },
    // 关闭弹窗
    close() {
      this.$emit('update:showAssignDialog', false)
      //   清空选中状态
      this.checkList = []
    }
  }
}
</script>

<style>

</style>
